/*
 * File:   Viewport.h
 * Author: ranjeet
 *
 * Created on January 4, 2009, 11:47 AM
 */

#ifndef _VIEWPORT_H
#define	_VIEWPORT_H

#include <iostream>
#include <stdexcept>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

#include "Debug.h"
#include "Sint.h"


using namespace std;

/**
 *\brief Viewport class
 *
 *The class that does all the rendering
 */

class Viewport {
protected:
    static Viewport* instance;
public:
    ///The main background layer aka layer0
    SDL_Surface *surface;

    ///x Co-ordiname of Viewport in world
    SyInt wx;

    ///y Co-ordiname of Viewport in world
    SyInt wy;

    int renderer_wx;
    int renderer_wy;

    ///Width of ViewPort
    int width;

    ///Height of Viewport
    unsigned int height;

    ///Bits_per_pixel
    int bits_per_pixel;

    ///Screen refresh rate
    unsigned int refresh_rate;

    ///response_rate
    unsigned int response_rate;

    static bool keep_looping;

    ///Global Colorkey
    Uint32 colorkey;

    /**
     *\brief kills Viewport
     *
     *At present just frees SDL surface
     *
     *In future may do some additional cleanup
     */
    static void destroy();

    /**
     *\brief updates Viewport
     *
     *Calls SDL_UpdateRect
     *may call swap buffers
     */
    void update();

    ///Increments Viewport's x co-ordinate
    void increment_wx(int incVal = 1);

    ///Increments Viewport's y co-ordinate
    void increment_wy(int incVal = 1);

    ///Decrements Viewports x co-ordinate
    void decrement_wx(int incVal = 1);

    ///Decrements Viewports x co-ordinate
    void decrement_wy(int incVal = 1);

    ///The renderer thread
    void render();
    SDL_PixelFormat *pixelformat();
    SDL_Surface *get_surface();


    static Viewport* get_instance();

    ~Viewport();
    /**
     *\breif Costructor viewport
     *
     *Calls SDL_Init()
     *Calls atexit(SDL_Quit)
     *Sets VideoMode...width, height, bits_per_pixel, flags, refresh rate
     *
     *\TODO
     *Initialization of layers
     */
    Viewport(int _width = 640, int _height = 480, int _bits_per_pixel = 16,
            Uint32 flags = SDL_DOUBLEBUF | SDL_HWSURFACE,
            unsigned short int _refresh_rate = 15);
};

inline SDL_PixelFormat *Viewport::pixelformat() {
    return surface->format;
}

inline SDL_Surface *Viewport::get_surface() {
    return surface;
}

#endif	/* _VIEWPORT_H */
